package com.app.micaihu.bean.news;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.ad.AdConfig;
import com.app.micaihu.custom.components.downandupload.DownloadUtils;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.service.DownLoadService;
import com.app.micaihu.videoplayer.JCVideoPlayer;
import com.app.micaihu.videoplayer.JCVideoPlayerGame;
import com.app.micaihu.view.main.game.GameDownloadActivity;
import com.app.utils.util.AppTools;
import com.app.utils.util.image.LoadImageUtils;
import com.baidu.mobstat.StatService;

/**
 * 视频大图(视频列表)
 * Created by linkai on 2016/7/18.
 */
public class ViewGameVideoTwo extends ViewNewsParent implements View.OnClickListener{

    public JCVideoPlayerGame jcVideoPlayerStandard;
    public CustomImageView userHead;//用户头像
    public TextView userName;//用户昵称
    public TextView downloadView;//下载按钮

    private String downloadUrl;//下载连接
    private boolean isinstall;//是否安装
    private String packageName;//包名
    private String downloadName;//下载名称
    private String serialId;//文章后台位置id
    private NewsEntity nativeAd;
    public ViewGameVideoTwo(View convertView) {
        initView(convertView);
    }

    public void initView(View convertView) {
        super.initView(convertView);
        this.jcVideoPlayerStandard = (JCVideoPlayerGame) convertView.findViewById(R.id.item_videoplayer);
        this.jcVideoPlayerStandard.setShowTitle(true);
        this.userHead = (CustomImageView) convertView.findViewById(R.id.video_usericon);
        this.userName = (TextView) convertView.findViewById(R.id.video_username);
        downloadView = (TextView) convertView.findViewById(R.id.ad_download);
        LoadImageUtils.getInstance().loadImage(mark, R.drawable.infor_list_home_game, false);
        this.downloadView.setOnClickListener(this);
        convertView.setTag(this);
    }

    /**
     * 加载数据
     * @param newsEntity
     */
    public void loadData(NewsEntity newsEntity){
        this.nativeAd=newsEntity;
        super.loadDataNoMark(newsEntity);
        this.jcVideoPlayerStandard.setUp(newsEntity.getVideoCurl(), JCVideoPlayer.SCREEN_LAYOUT_LIST,
                newsEntity.getArticleTitle(),newsEntity.getArticleThumb()[0],newsEntity.getPlayTime());
        this.jcVideoPlayerStandard.setShareBean(newsEntity, newsEntity.getUrlStatus());
        LoadImageUtils.getInstance().loadListCircleImage(this.userHead, newsEntity.getAuthorHeadPic());
//        this.laudNum.setText(newsEntity.getPraiseNum());
//        this.laudNum.setOnClickListener(this);
//        this.laudNum.setTag(R.id.tag_first, newsEntity.getArticleId());
//        this.laudNum.setSelected(newsEntity.IsPraise());
//        this.laudNum.setTag(newsEntity.IsPraise());
        this.userName.setText(newsEntity.getAuthorName());
        this.userHead.setTag(newsEntity.getAuthorId());
        this.userName.setTag(newsEntity.getAuthorId());
        downloadUrl = newsEntity.getJumpUrl();
        downloadName = newsEntity.getAuthorName();
        isinstall= newsEntity.isinstall();
        packageName = newsEntity.getPackageName();
        serialId = newsEntity.getSerialId();
        if(isinstall){
            downloadView.setText("立即打开");
        }else{
            downloadView.setText("立即下载");
        }
    }


    @Override
    public void onClick(View v) {
        if(v == null){
            return;
        }
        if(v.getId() == R.id.ad_download){
            String type;
            if(isinstall){
                //立即打开
                AppTools.startUpApp(v.getContext(), packageName);
                type = AdConfig.TYPE_2;
            }else{
                //开始下载
                //插入到数据库中
                GameDownloadActivity.insertGame(nativeAd.getIGameId(), nativeAd.getITitle(), nativeAd.getIUrl(), nativeAd.getIAppIcon(),
                        nativeAd.getArticleSummary(), nativeAd.getPackageName(), nativeAd.getIPath(), nativeAd.getISize());
                DownloadUtils.getSingleton().down(nativeAd.getIUrl(), nativeAd.getIPath(), nativeAd);

//                DownLoadService.startDownLoadApk(this.mConvertView.getContext(),downloadUrl,downloadName);
                type = AdConfig.TYPE_3;
                StatService.onEvent(this.mConvertView.getContext(), "057", "视频列表视频游戏下载", 1);// 统计的点击的次数
            }
            if(!TextUtils.isEmpty(serialId)){
                AdConfig.staticsAd(serialId, type,AdConfig.AD_TYPE_GAME_VIDEO);
            }
        }

    }

}
